package com.ctgu.chatapp.utils

import com.ctgu.chatapp.Netty.protocol.Message.*

object MessageUtils {

    private fun getIMSMsg(
        msgType: Head.MsgType,
        fromID: Long, toID: Long
    ): IMSMsg.Builder {
        val timestamp = System.currentTimeMillis()
        val head = Head.newBuilder()
        head.msgType = msgType
        head.fromId = fromID
        head.toId = toID
        head.timestamp = timestamp
        val iMSMsg = IMSMsg.newBuilder()
        iMSMsg.head = head.build()
        return iMSMsg
    }

    fun getLoginRequest(userId: Long, password: String): IMSMsg {
        val request = getIMSMsg(Head.MsgType.Request, userId, 0)
        val loginRequest = LoginRequest.newBuilder()
        loginRequest.chatId = userId
        loginRequest.password = password
        request.loginRequest = loginRequest.build()
        request.bodyType = IMSMsg.BodyType.LoginRequest
        return request.build()
    }

    fun getSingleChatRequestText(
        context: String?,
        selfID: Long,
        friendID: Long,
    ): IMSMsg {
        val request = getIMSMsg(Head.MsgType.Request, selfID, friendID)
        val singleRequest = ChatRequest.newBuilder()
        singleRequest.messageType = ChatRequest.Type.SingleMessage
        singleRequest.contentType = ChatRequest.ContextType.word
        singleRequest.context = context
        request.chatRequest = singleRequest.build()
        request.bodyType = IMSMsg.BodyType.ChatRequest
        return request.build()
    }

    fun getSingleChatRequestFile(
        selfID: Long, friendID: Long, file: String?, FileName: String?
    ): IMSMsg {
        val request = getIMSMsg(Head.MsgType.Request, selfID, friendID)
        val singleRequest = ChatRequest.newBuilder()
        singleRequest.messageType = ChatRequest.Type.SingleMessage
        singleRequest.contentType = ChatRequest.ContextType.picture
        singleRequest.fileMessage =
            FileMessage.newBuilder().setFileName(FileName).setFileSize(file).build()
        request.chatRequest = singleRequest.build()
        request.bodyType = IMSMsg.BodyType.ChatRequest
        return request.build()
    }

    fun getHeartBeat(): IMSMsg {
        return getIMSMsg(Head.MsgType.Heart, 0, 0).setHeart(Heartbeat.newBuilder().build())
            .setBodyType(IMSMsg.BodyType.HeartBeat)
            .build()
    }

    fun getAck(id: Long): IMSMsg {
        return getIMSMsg(Head.MsgType.Acknowledge, Utils.chatID, 0)
            .setChatAck(ChatAck.newBuilder().setId(id).build()).build()
    }

    fun getFriendRequest(uid: Long, touid: Long, type: String): IMSMsg {
        val request = getIMSMsg(Head.MsgType.Request, uid, touid)
        request.friendRequest = FriendRequest.newBuilder().setType(type).build()
        request.bodyType = IMSMsg.BodyType.FriendRequest
        return request.build()
    }

    fun getFriendResponse(uid: Long, touid: Long, type: String, id: Int): IMSMsg {
        val response = getIMSMsg(Head.MsgType.Request, uid, touid)
        response.friendResponse = FriendResponse.newBuilder().setId(id).setType(type).build()
        response.bodyType = IMSMsg.BodyType.FriendResponse
        return response.build()
    }

    fun getControlRequest(uid: Long, touid: Long, snowFlowId: Long): IMSMsg {
        val request = getIMSMsg(Head.MsgType.Request, uid, touid)
        val singleRequest = ChatRequest.newBuilder()
        singleRequest.messageType = ChatRequest.Type.SingleMessage
        singleRequest.contentType = ChatRequest.ContextType.Control
        singleRequest.snowFlower = snowFlowId
        request.chatRequest = singleRequest.build()
        request.bodyType = IMSMsg.BodyType.ChatRequest
        return request.build()
    }
}